#include "mprpcapplication.h"
#include "rpcprovider.h"
#include "user.pb.h"
#include <google/protobuf/port_def.inc>
#include <iostream>
#include <string>

class UserService : public fixbug::UserServiceRpc {
public:
  bool Login(std::string name, std::string pwd) {
    std::cout << "doing local service: Login" << std::endl;
    std::cout << "name: " << name << " "
              << "pwd: " << pwd << std::endl;
    return true;
  }
  bool Registry(uint32_t id, std::string name, std::string pwd) {
    std::cout << "doing local service: Registry" << std::endl;
    std::cout << "name: " << name << " "
              << "pwd: " << pwd << std::endl;
    return true;
  }
  void Login(::PROTOBUF_NAMESPACE_ID::RpcController *controller,
             const ::fixbug::LoginRequest *request,
             ::fixbug::LoginResponse *response,
             ::google::protobuf::Closure *done) override {
    // 获取参数
    std::string name = request->name();
    std::string pwd = request->pwd();
    // 本地执行
    bool login_result = Login(name, pwd);
    // 返回结果
    fixbug::ResultCode *code = response->mutable_result();
    code->set_errcode(0);
    code->set_errmsg("");
    response->set_success(login_result);
    // 调用回调操作
    done->Run();
  }
  void Registry(::PROTOBUF_NAMESPACE_ID::RpcController *controller,
                const ::fixbug::RegistryRequest *request,
                ::fixbug::RegistryResponse *response,
                ::google::protobuf::Closure *done) override {
    // 获取参数
    uint32_t id = request->id();
    std::string name = request->name();
    std::string pwd = request->pwd();
    // 本地执行
    bool login_result = Registry(id, name, pwd);
    // 返回结果
    fixbug::ResultCode *code = response->mutable_result();
    code->set_errcode(0);
    code->set_errmsg("");
    response->set_success(login_result);
    // 调用回调操作
    done->Run();
  }
};

int main(int argc, char **argv) {
  // 框架初始化
  MprpcApplication::Init(argc, argv);
  // 把 UserService 对象发布到 rpc 节点上
  RpcProvider provider;
  provider.NotifyService(new UserService());
  // 执行进行阻塞态，等待调用
  provider.Run();
  return 0;
}
